package com.demo.oauth2jFinalShiroServer.web;


import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;

import com.jfinal.core.ActionKey;
import com.jfinal.core.Controller;


public class LoginController extends Controller {
	@ActionKey("/")
    public void index() {
		renderJsp("/index.jsp");
	}
	/**
	 * 本站登录(GET|POST)
	 * @author: jie.deng
	 */
	@ActionKey("login")
    public void login() {
    	HttpServletRequest req = getRequest();
		if (req.getMethod().equalsIgnoreCase("GET")) {
    		renderJsp("/login.jsp");
		}else {
			//登录成功,直接跳转到默认页面或者登录之前的页面，不执行下面的代码
			//登录失败,执行下面的代码
			String className = String.valueOf(getAttr(FormAuthenticationFilter.DEFAULT_ERROR_KEY_ATTRIBUTE_NAME));
			if (UnknownAccountException.class.getName().equals(className) || IncorrectCredentialsException.class.getName().endsWith(className)) {
				setAttr(FormAuthenticationFilter.DEFAULT_ERROR_KEY_ATTRIBUTE_NAME,"用户名/密码错误");
			}else if (AuthenticationException.class.getName().endsWith(className)) {
				setAttr(FormAuthenticationFilter.DEFAULT_ERROR_KEY_ATTRIBUTE_NAME,"其他错误");
			}
			renderJsp("/login.jsp");
		}
    }
    

}
